import { RemoveMethods } from "@/types/remove-methods";
import { TaskLogData } from "@model/TaskLogMap";
import UseSetting from "@pinias/UseSetting";
import pinia from "@pinias/pinia";
import { DateTimeStr } from "@utils/DateUtils";
import { memoryPath } from "@utils/pathUtils";

export class YearMemory {
  key: string; // 2023
  memoryMap: Map<string, Array<string>>; // 202308:[20230819121503...] 列表按顺序排列

  static add2SortList(id: string, list: Array<string>) {
    const logTimeStamp = DateTimeStr.getTimeStamp(id);
    let index = -1;
    for (let j = 0; j < list.length; j++) {
      const time = list[j];
      const timeStamp = DateTimeStr.getTimeStamp(time);
      if (timeStamp > logTimeStamp) {
        index = j;
        break;
      }
    }
    if (index !== -1) list.splice(index, 0, id);
    else list.push(id);
    return list;
  }

  constructor(key: string) {
    this.key = key;
    this.memoryMap = new Map<string, Array<string>>();
  } // 20230723003406...
}
const { getCacheModel } = UseSetting(pinia);
export class CacheMemory {
  cacheMemoryFileList: Array<string>; // 2023
  cacheMemoryMap: Map<string, YearMemory>; // 2023:model
  changeKeys: Array<string>;

  setInfo(data: RemoveMethods<CacheMemory>) {
    Object.assign(this, data);
  }
  constructor(cacheMemoryFileList: Array<string>, cacheMemoryMap: Map<string, YearMemory>, changeKeys: Array<string>) {
    this.cacheMemoryFileList = cacheMemoryFileList;
    this.cacheMemoryMap = cacheMemoryMap;
    this.changeKeys = changeKeys;
  }
  async getMemory4MonthList(monthRageTimeStamp: Array<number>) {
    const result = new Array<string>();
    for (const timeStamp of monthRageTimeStamp) {
      const timeStr = new DateTimeStr(timeStamp);
      const key = timeStr.getYearInfo();
      const monthKey = timeStr.getMonthInfo();
      let yearMemory = new YearMemory(key);
      // console.log(endTime)
      yearMemory = await getCacheModel(memoryPath + key + ".json", key, this.cacheMemoryMap, yearMemory);
      const taskLogIds = yearMemory.memoryMap.get(monthKey);
      // console.log(taskLogIds,monthKey,yearMemory)
      if (taskLogIds !== undefined && taskLogIds.length > 0) result.push(...taskLogIds);
    }
    return result;
  }
  async addMemory(taskLogData: Array<TaskLogData>) {
    const changeKeys = new Set<string>();
    for (let i = 0; i < taskLogData.length; i++) {
      const log = taskLogData[i];
      const key = log.id.slice(0, 4);
      let yearMemory = new YearMemory(key);
      // console.log(endTime)
      yearMemory = await getCacheModel(memoryPath + key + ".json", key, this.cacheMemoryMap, yearMemory);
      const monthKey = log.id.slice(0, 6);
      let list = yearMemory.memoryMap.get(monthKey);
      if (list === undefined) {
        list = new Array<string>();
        yearMemory.memoryMap.set(monthKey, list);
      }
      YearMemory.add2SortList(log.id, list);
      yearMemory.memoryMap.set(monthKey, list);
      changeKeys.add(yearMemory.key);
    }
    this.changeKeys.push(...changeKeys);
  }
  async deleteMemory(taskLogData: Array<TaskLogData>) {
    const changeKeys = new Set<string>();
    for (let i = 0; i < taskLogData.length; i++) {
      const log = taskLogData[i];
      const key = log.id.slice(0, 4);
      let yearMemory = new YearMemory(key);
      // console.log(endTime)
      yearMemory = await getCacheModel(memoryPath + key + ".json", key, this.cacheMemoryMap, yearMemory);
      const monthKey = log.id.slice(0, 6);
      let list = yearMemory.memoryMap.get(monthKey);
      if (list !== undefined) {
        list = list.filter(value => value !== log.id);
        yearMemory.memoryMap.set(monthKey, list);
      }
      changeKeys.add(yearMemory.key);
    }
    this.changeKeys.push(...changeKeys);
  }
}
